import api_home from '@/api/api_home'
import sys_manage from '@/api/sys_manage'
import * as Constant from '@/utils/constant'
import {dateFtt} from '@/utils/datetime'
import {ALERT} from '@/utils/responseHandle'
// initial state
const state = {
  noticeList: [],
  processList: [],
  processGridsPage: [], // 应用流程（分页）
  functionGirdData: [], // 常用功能
  functionGirdPage: [
    { cols: [] }
  ]
}

// getters
const getters = {

}

// actions
const actions = {
  getNoticeList ({ commit, state }, {_this, payload}) {
    let results = []
    sys_manage.sysTipsLoadByPaging(payload, response => {
      if (response.code === Constant.RT_SUCCESS) {
        results = response.data.results
      }
      commit('setNoticeList', results)
    })
  },
  getProcessList ({ commit, state }, {_this, payload}) {
    let results = []
    api_home.getProcessList(payload, response => {
      if (response.code === Constant.RT_SUCCESS) {
        results = response.data.results
      }
      commit('setProcessList', results)
    })
  },
  // 应用流程（分页）
  sysProcessLoadByPaging ({ commit, state }, processType) { // 获取流程分类对应的流程列表
    let q = {
      processType,
      pageNum: 1,
      pageSize: 10000
    }
    let results = []
    sys_manage.sysProcessLoadByPaging(q, response => {
      if (response.code === Constant.RT_SUCCESS) {
        results = response.data.results
      }
      commit('setFunctionGirdDataProcess', results)
    })
  },
  getSysAppLoadByPaging ({ commit, state }, data) { // 获取系统分类对应的系统应用
    let results = []
    api_home.functionLoadAll({systemType: data}, response => {
      if (response.code === Constant.RT_SUCCESS) {
        results = response.data
      }
      commit('setFunctionGirdData1', results)
    })
  },
  functionLoadUseList ({ commit, state }, {_this, payload}) { // 获取常用功能
    let results = []
    api_home.functionLoadUseList(null, response => {
      if (response.code === Constant.RT_SUCCESS) {
        results = response.data
      }
      commit('setFunctionGirdData', results)
    })
  },
  saveUsualFunc ({ dispatch, state, commit }, {_this, data}) { // 保存常用功能
    api_home.functionSave(data, response => {
      if (response.code === Constant.RT_SUCCESS) {
        dispatch('functionLoadUseList', {})
        _this.closeFunctionConfig()
      } else {
        ALERT.error(response.msg)
      }
      _this.closeFunctionSubmiting()
    })
  }
}

// mutations
const mutations = {
  setNoticeList (state, results) {
    state.noticeList = results.map(function (item) {
      return item.content
    })
  },
  setProcessList (state, results) {
    state.processList = results.map(function (item) {
      let d = {
        value: item.id,
        title: item.processName,
        extra: dateFtt('yyyy-MM-dd', item.visitTime)
      }
      return d
    })
  },
  setFunctionGirdData (state, results) {
    state.functionGirdData = results.map(function (item) {
      return {
        systemName: item.systemName,
        systemInfoId: item.systemInfoId,
        systemConfigType: item.systemConfigType,
        systemUrl: item.systemUrl,
        imageCode: item.imageCode ? item.imageCode : 'scp_home_debug'
      }
    })
    state.functionGirdData.push({
      systemName: '设置',
      systemInfoId: '100',
      imageCode: 'scp_home_setting'
    })
    let dataList = JSON.parse(JSON.stringify(state.functionGirdData))
    let pagingPageSize = 9
    let num = dataList.length / pagingPageSize
    let rownum = Math.ceil(num)
    let rows = Array(rownum).fill({ cols: [] })
    for (var r = 0; r < rownum; r++) {
      let cols = [] // 这里是不能使用Array(this.cols).fill({}) 填充的(最后一行数据有可能不能填满)
      for (var d = 0; d < pagingPageSize; d++) {
        let item = dataList[0]
        if (!item) {
          continue
        }
        cols[d] = item
        dataList.shift()
      }
      rows[r] = { cols: cols }
    }
    state.functionGirdPage = rows
  },
  setFunctionGirdData1 (state, results) {
    state.functionGirdData = results.map(function (item) {
      return {
        systemName: item.systemName,
        systemInfoId: item.id,
        systemType: item.systemType,
        systemUrl: item.systemUrl,
        imageCode: item.imageCode ? item.imageCode : 'scp_home_debug'
      }
    })
    let dataList = JSON.parse(JSON.stringify(state.functionGirdData))
    let pagingPageSize = 9
    let num = dataList.length / pagingPageSize
    let rownum = Math.ceil(num)
    let rows = Array(rownum).fill({ cols: [] })
    for (var r = 0; r < rownum; r++) {
      let cols = [] // 这里是不能使用Array(this.cols).fill({}) 填充的(最后一行数据有可能不能填满)
      for (var d = 0; d < pagingPageSize; d++) {
        let item = dataList[0]
        if (!item) {
          continue
        }
        cols[d] = item
        dataList.shift()
      }
      rows[r] = { cols: cols }
    }
    state.functionGirdPage = rows
  },
  setFunctionGirdDataProcess (state, results) { // 应用流程（分页）
    let dataList = results
    let pagingPageSize = 10
    let num = dataList.length / pagingPageSize
    let rownum = Math.ceil(num)
    let rows = Array(rownum).fill({ cols: [] })
    for (var r = 0; r < rownum; r++) {
      let cols = [] // 这里是不能使用Array(this.cols).fill({}) 填充的(最后一行数据有可能不能填满)
      for (var d = 0; d < pagingPageSize; d++) {
        let item = dataList[0]
        if (!item) {
          continue
        }
        cols[d] = item
        dataList.shift()
      }
      rows[r] = { cols: cols }
    }
    state.processGridsPage = rows
  },
}

export default {
  namespaced: true,
  state,
  getters,
  actions,
  mutations
}
